package com.example.flshtarvel.controller;



import com.example.flshtarvel.entity.Collect;
import com.example.flshtarvel.service.CollectService;
import com.example.flshtarvel.service.RouteCollectMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CollectController {
//    @Autowired
//    RouteCollectMapService routeCollectMapService;
    @Autowired
    CollectService collectService;

    //路线收藏
    //@GetMapping("/routeCollect/{routeId}")
    @PostMapping("/collect")
    public Map<String,Object> routeCollect(Collect collect, HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        collect.setUserId((String)session.getAttribute("userId"));
        int result = collectService.collect(collect);
        if(result==1){
            map.put("msg",1);
        }else{
            map.put("msg",0);
        }
        return map;
    }
    //取消收藏
    @GetMapping("/deleteCollect")
    public Map<String,Object> deleteCollect(@RequestParam("id")String messageId, HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        int result = collectService.deleteCollect(messageId,(String)session.getAttribute("userId"));
        if(result >= 1){
            map.put("msg",1);
        }else{
            map.put("msg",0);
        }
        return map;
    }
    //查询收藏    根据路线id 用户id
    @GetMapping("/seleteCollect")
    public Map<String,Object> selectCollectByMessageIdAndUserId(@RequestParam("messageId")String messageId,
                                                                   HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        int result = collectService.selectCollectByMessageIdAndUserId(messageId,(String)session.getAttribute("userId"));
        if(result==1){
            System.out.println("1");
        }else{
            System.out.println("0");
        }
        return map;
    }

    //查询收藏数   根据路线id
    @GetMapping("/seleteCollectCount")
    public Map<String,Object> selectCollectByMessageId(String messageId){
        Map<String,Object> map = new HashMap<String,Object>();
        int result = collectService.selectCollectByMessageId(messageId);
        map.put("msg",1);
        map.put("count",result);
        return map;
    }

    //查询收藏 根据用户id
    @GetMapping("/selectCollectByUserId")
    public Map<String,Object> selectCollectByUserId(HttpSession session){
        Map<String,Object> map = new HashMap<>();
        List<Collect> collectList = collectService.selectCollectByUserId((String)session.getAttribute("userId"));
        if(collectList.size()<=0){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("resultList",collectList);
        }
        return map;
    }


}

